/**
 * Manages creates, edits, deletes, and generally handles ToDoItems.
 */
package edu.gatech.stolen.tea;

import java.util.ArrayList;

public class ItemManager {
	
	private static User currentUser;
	private static ToDoItem currentItem;
	private static ItemHolder userItems = new ItemHolder();
	//private static boolean initialized = false;
	
	public ItemManager(String currentUser) {
	}
	
	public static void initialize() {
		
		userItems = new ItemHolder();
		
	}
	
	/**
	 * sets the item that is to be edited, viewed, or deleted
	 * 
	 * @param item - item the app is to focus on
	 */
	public static void setCurrentItem(ToDoItem item) {
		
		ItemManager.currentItem = item;
		
	}
	
	/**
	 * gets the item that is to be edited, viewed, or deleted
	 * 
	 * @return item that app is focusing on
	 */
	public static ToDoItem getCurrentItem() {
		
		return currentItem;
		
	}
	
	/**
	 * makes the app have no item in focus
	 */
	public static void currentItemReset() {
		
		currentItem = null;
		
	}
	
	 /**
	  * creates new items
	  * 
	  * @param itemName - name of item
	  * @param itemDescription - description of item
	  * @param itemLocation - location of item
	  * @param monthGiven - month the item was given to user
	  * @param monthDue - month the item is to be done by the user
	  * @param type - type of item
	  * 
	  * @return true if the item was successfully added to collection if items, false otherwise
	  */
	public static boolean createItem(String itemName, String itemDescription, String itemLocation, Month monthGiven, Month monthDue, ItemType type) {
		
		ToDoItem item = new ToDoItem();
		item.setName(itemName);
		item.setDescription(itemDescription);
		item.setLocation(itemLocation);
		item.setDateGiven(monthGiven);
		item.setDueDate(monthDue);
		item.setType(type);
		item.setDone(false);
		
		return userItems.add(item);
		
	}
	
	/**
	 * edits the item
	 * 
	 * @param what - indicates what attribute of item is to be edited
	 * @param data - the data the attribute is to be changed to
	 * @param item - the item being edited
	 */
	public static void editItem(editItemEnum what, Object data, ToDoItem item) {
		
		what.edit(data, item);
		
	}
	
	/**
	 * edits the item
	 * 
	 * @param what - indicates what attribute of item is to be edited
	 * @param data - the data the attribute is to be changed to
	 * @param item - the item being edited
	 */
	public static void editItem(editItemEnum what, boolean data, ToDoItem item) {
		
		what.editDone(data, item);
		
	}
	
	/**
	 * deletes new items
	 * 
	 * @return true if the item was successfully added to collection if items, false otherwise
	 */
	public static boolean deleteItem() {
		
		ToDoItem item = currentItem;
		currentItemReset();
		return userItems.remove(item);
		
	}
	
	/**
	 * sets the owner of the items
	 * 
	 * @param u - owner of the items
	 */
	public static void setUser(User u) {
		
		ItemManager.currentUser = u;
		
	}
	
	/**
	 * gets the owner of the items
	 * 
	 * @return the owner off the items
	 */
	public static User getUser() {
		
		return currentUser;
		
	}
	
	public static ArrayList<ToDoItem> getUserItems(){
		
		return userItems.getAll();
		
	}
	
	
	/**
	 * gets the ToDoItems that user owns
	 */
	public static ArrayList<ToDoItem> getUserItems(ItemType type) {
		
		return userItems.getType(type);
		
	}
	
	/**
	 * gets a ToDoItem that user owns based on its name
	 */
	public static ToDoItem getUserItem(ToDoItem item) {
		
		return userItems.getItem(item);
		
	}
	
	/**
	 * checks to see if a ToDoItem exists
	 */
	public static boolean contains(ToDoItem item) {
		
		if (currentItem.getName().equals(item.getName())){
			if (currentItem.getDescription().equals(item.getDescription())){
				if (currentItem.getLocation().equals(item.getLocation())){
					if (currentItem.getDateGiven().equals(item.getDateGiven())){
						if (currentItem.getDueDate().equals(item.getDueDate())){
							return true;
						}
					}
				}
			}
		}
		
		return false;
		
	}
	
	public static ItemHolder getHolder(){
		
		return userItems;
		
	}
	
}
